function CardCreator(){
    function createTag(tag, className){
        var box = document.createElement(tag);
        if (!util.isNullString(className)) {
            box.setAttribute("class", className);
        }
        return box;
    }
    function createKV(className, key, value){
        var box = createTag("div", className);
        var k = createTag("span", "key");
        k.innerText = key;
        var v = createTag("span", "value");
        v.innerHTML = value;
        v.title = value;
        box.appendChild(k);
        box.appendChild(v);
        return box;
    }
    function getParticipation(p){
        var r = "";
        for (var i = 0, l = p.length; i < l; i++) {
            var item = p[i];
            r = r + " / " + util.getArrayDefault(item, "name", "");
        }
        return r.substring(3);
    }
    function createFlod(){
        var box = createTag("div", "fold_episode_block_hidden");
        var a = createTag("span", "logoName");
        a.setAttribute("href", "javascript:void(0);");
        a.innerHTML = "收起";
        box.appendChild(a);
        return box;
    }
    function createPlatform(logo, name){
        var box = createTag("div", "logoBox");
        var i = createTag("i", "logo "+logo);
        var em = createTag("span", "logoName");
        em.innerHTML = name;
        box.appendChild(i);
        box.appendChild(em);
        return box;
    }
    function createEpisodeBlock( episode ){
        var name = util.getArrayDefault(episode,"name", "");
        var url = util.getArrayDefault(episode,"url", "");
        var title = util.getArrayDefault(episode,"title", "");
        var className = util.getArrayDefault(episode,"className", "");
        var box = createTag("li", "episode_block " + className);
        if (!util.isNullString(title)){
            box.title = title;
        }
        var a = createTag("a");
        a.setAttribute("target", "_blank");
        a.setAttribute("href", "javascript:void(0)");
        a.setAttribute("url", url);
        a.setAttribute("platform", episode.platform);
        a.setAttribute("tvid", episode.id);
        a.innerHTML = name;
        box.appendChild(a);
        return box;
    }
    function createBaseInfo(mate, desc){
        var director = util.getArrayDefault(mate,"director",[]);
        var actor = util.getArrayDefault(mate,"actor",[]);
        var guest = util.getArrayDefault(mate,"guest",[]);
        var box = createTag("div", "baseInfo");
        if (!util.isEmptyArray(guest)) {
            var guestBox = createKV("infoItem half", "嘉宾：", getParticipation(guest));
            box.appendChild(guestBox);
        }
        if (!util.isEmptyArray(director)) {
            var directorBox = createKV("infoItem half", "导演：", getParticipation(director) );
            box.appendChild(directorBox);
        }
        if (!util.isEmptyArray(actor)) {
            var actorBox = createKV("infoItem half", "演员：", getParticipation(actor) );
            box.appendChild(actorBox);
        }
        if (!util.isNullString(desc)) {
            var directorBox = createKV("infoItem", "简介：", desc.trim());
            box.appendChild(directorBox);
        }
        return box;
    }
    function createTitle(videoName, classTag, year){
        var box = createTag("div");
        if (!util.isNullString(classTag)) {
            var span = createTag("span", "classTag");
            span.innerHTML = classTag;
            box.appendChild(span);
        }
        var videoNameBox = createTag("span", "videoName");
        videoNameBox.innerHTML = videoName;
        box.appendChild(videoNameBox);
        if (!util.isNullString(year)) {
            var span = createTag("span", "year");
            span.innerHTML = year;
            box.appendChild(span);
        }
        return box;
    }
    function createCoverBox(src, mainUrl, mate, platform, config){
        var box = createTag("a", "coverImgBox");
        box.setAttribute("href", "javascript:void(0);");
        box.setAttribute("url", mainUrl);
        box.setAttribute("platform", platform);

        var img = createTag("img", "coverImg");
        img.setAttribute("referrerPolicy", "no-referrer");
        img.setAttribute("src", src);
        img.setAttribute("title", "视频封面");
        if (config && config.errImg) {
            if (img.style) {
                img.style = {};
            }
            img.style.backgroundImage = "url('" + config.errImg + "')";
            img.setAttribute("onerror", "$(this).attr('src', '"+config.errImg+"')");
        }
        box.appendChild(img);
        if (!util.isNullString(mate)) {
            var mateInfo = createTag("span", "mate");
            mateInfo.innerHTML = mate;
            box.appendChild(mateInfo);
        }
        return box;
    }
    function dealNoVideoList(item){
        // item.tag == "电影" &&
        if (util.isEmptyArray(item.videoInfos)){
            item.videoInfos = [];
            item.videoInfos.push({title:"立即播放", number:"立即播放", platform:item.platform, url: item.url, className: "btn_implay" });
        }
        return item;
    }
    function dealDisplayMode(box, list, displayMode){
        if (displayMode == 1){
            return  dealHalfBlockDisplay( list );
        }
        if (displayMode == 2){
            return dealBlockTitleDisplay(box, list);
        }
        return dealBlockDisplay(box,list);
    }
    function dealBlockTitleDisplay( box, list ){
        var size = list.length;
        for (var i = 0; i < size; i++) {
            var item = list[i];
            item.className = "episode_block_display btn_title_play";
            item.name = item.title;
        }
        return list;
    }
    function dealDisplayPhoneMode(box, list, displayMode){
        return  dealBlockDisplay(box,list, 2);
    }
    function dealHalfBlockDisplay( list ){
        var maxLine = 3;
        var size = list.length;

        for (var i = 0; i < size; i++) {
            var item = list[i];
            if (i < 2 * maxLine - 1) {
                item.className = "episode_block_displayHalf " + util.getArrayDefault(item, "className", "");
            } else {
                item.className = "episode_block_displayHalf episode_block_hidden " + util.getArrayDefault(item, "className", "");
            }
            if (util.isNullString(item.date)){
                item.name = item.title;
            }
            else {
                item.name = item.date + ": " + item.title;
            }
        }
        if (2*maxLine - 1 < size){
            list.splice(2*(maxLine)-1, 0, {"title":"更多", "name":"更多>>", "className":"episode_block_more episode_block_displayHalf"});
        }
        return list;
    }
    function dealBlockDisplay(box, list, maxLine){
        if (util.isEmptyArray(box.classList) || !box.classList.contains("episode_block_box")) {
            box = box.getElementsByClassName("episode_block_box")[0];
        }
        maxLine = util.getDefault(maxLine, 3);
        var count = parseInt(box.clientWidth/46);
        var size = list.length;
        for (var i = 0 ; i < size; i++) {
            var item = list[i];
            if(i < count*(maxLine-1) - 1 || i > size - 1 - count ){
                item.className = "episode_block_display " + util.getArrayDefault(item, "className", "");
            }
            else{
                item.className = "episode_block_display episode_block_hidden " + util.getArrayDefault(item, "className", "");
            }
            item.name = item.number;
        }
        if (size > count*maxLine ) {
            list.splice(count*(maxLine-1)-1, 0, {"title":"展开", "name":"...", "className":"episode_block_more episode_block_display"});
        }
        return list;
    }
    return {
        createPositionCard:function(isPhone){
            var box = createTag("div", "card");
            var cover = createCoverBox();
            box.appendChild(cover);
            var infoBox = createTag("div", "infoBox");
            var div = createTag("div");
            var title = createTitle();
            div.appendChild(title);
            var baseInfo = createBaseInfo({"director":[{name:"测试"}]}, "测试");
            div.appendChild(baseInfo);
            infoBox.appendChild(div);
            var episodeBox = createTag("ul", "episode_block_box");
            episodeBox.appendChild(createTag("br", "clearFloat"));
            if (isPhone !== true){
                baseInfo.appendChild(episodeBox);
            }
            baseInfo.appendChild(createFlod());
            baseInfo.appendChild(createPlatform());
            box.appendChild(infoBox);
            if (isPhone === true){
                box.appendChild(episodeBox);
            }
            return box;
        },
        createCard: function(data, positionCard, config){
            data = dealNoVideoList(data);
            var box = createTag("li", "card");
            box.setAttribute("index", data.index);
            box.setAttribute("docid", data.id);
            var cover = createCoverBox(data.img, data.url, data.videoMate, data.platform, config);
            box.appendChild(cover);
            var infoBox = createTag("div", "infoBox");
            var div = createTag("div");
            var title = createTitle(data.name, data.tag, data.year);
            div.appendChild(title);
            var baseInfo = createBaseInfo(data.mate, data.desc);
            div.appendChild(baseInfo);
            infoBox.appendChild(div);
            var episodeBox = createTag("ul", "episode_block_box");
            var list = util.getArrayDefault(data, "videoInfos",[]);
            list = dealDisplayMode(positionCard, list, data.displayMode);
            for (var i = 0, l = list.length; i < l; i++) {
                var item = list[i];
                episodeBox.appendChild( createEpisodeBlock(item) );
            }
            episodeBox.appendChild(createTag("br", "clearFloat"));
            baseInfo.appendChild(episodeBox);
            baseInfo.appendChild(createFlod());
            baseInfo.appendChild(createPlatform(data.platform, data.platformName));
            box.appendChild(infoBox);
            return box;
        },
        createPhoneCard: function(data, positionCard, config){
            data = dealNoVideoList(data);
            var box = createTag("li", "card");
            box.setAttribute("index", data.index);
            box.setAttribute("docid", data.id);
            var cover = createCoverBox(data.img, data.url, data.videoMate, data.platform, config);
            box.appendChild(cover);
            var infoBox = createTag("div", "infoBox");
            var div = createTag("div");
            var title = createTitle(data.name, data.tag, data.year);
            div.appendChild(title);
            var baseInfo = createBaseInfo(data.mate, data.desc);
            div.appendChild(baseInfo);
            infoBox.appendChild(div);
            var episodeBox = createTag("ul", "episode_block_box");
            var list = util.getArrayDefault(data, "videoInfos",[]);
            list = dealDisplayPhoneMode(positionCard, list, data.displayMode);
            var saveLeft = false;
            for (var i = 0, l = list.length; i < l; i++) {
                var item = list[i];
                episodeBox.appendChild( createEpisodeBlock(item) );
                var className = util.getArrayDefault(item, "className", "");
                if ( className.indexOf("btn_implay" )!=-1){
                    saveLeft = true;
                    break;
                }
            }
            episodeBox.appendChild(createTag("br", "clearFloat"));
            if (saveLeft){
                baseInfo.appendChild(episodeBox);
            }
            baseInfo.appendChild(createFlod());
            baseInfo.appendChild(createPlatform(data.platform, data.platformName));
            box.appendChild(infoBox);
            if (!saveLeft){
                box.appendChild(episodeBox);
            }
            return box;
        }
    }
}